<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>自美系统升级</title>
    <link href="/static/bootstrap/bootstrap.min.css?v=3.3.7" rel="stylesheet">
    <link href="../../css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="../../css/animate.min.css?v=4.0.0" rel="stylesheet">
    <link href="../../css/style.min.css?v=4.0.0" rel="stylesheet">

    <link href="../../css-user/system.css" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="container-fluid topmenu">
        <h2><i class="fa fa-rocket"></i> 自美系统升级</h2>
    </div>
    <div class="container-fluid system-edit">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>自美系统一键升级工具</h5>
                    </div>
                    <div class="ibox-content">

                        <form class="form-horizontal" id="systemUpdate">
                            <div class="form-group">
                                <label for="version" class="col-sm-2 control-label">
                                    本地版本号
                                    <p class="text-muted">version</p>
                                </label>
                                <div class="col-sm-10">
                                    <p class="form-control-static" id="version"></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="version" class="col-sm-2 control-label">
                                    远程版本号
                                    <p class="text-muted">originVersion</p>
                                </label>
                                <div class="col-sm-10">
                                    <p class="form-control-static" id="origin_version"></p>
                                </div>
                            </div>
                            <div class="form-group" id="ProgressBox" style="display: none;">
                                <label for="version" class="col-sm-2 control-label">
                                    升级进度
                                    <!-- <p class="text-muted">Progress</p> -->
                                </label>
                                <div class="col-sm-10">
                                    <div class="progress progress-striped active">
                                        <div id="prog" style="width: 0%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0" role="progressbar" class="progress-bar progress-bar-danger">
                                            <span class="sr-only">40% Complete (success)</span>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="button" class="btn btn-success" disabled="disabled"
                                        id="isUpdate">无需升级</button>
                                </div>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <script src="/static/jquery/jquery.min.js?v=2.1.4" type="text/javascript"></script>
    <script src="/static/bootstrap/bootstrap.min.js?v=3.3.7" type="text/javascript"></script>
    <script src="../../js/content.js?v=1.0.0"></script>

    <!-- 自定义全局js -->
    <script src="../../js-user/public.js" type="text/javascript"></script>

    <!-- 自定义js -->
    <script>
        $(function () {
            // 加载本地版本号
            (load_localver = function () {
                var isUpdate = $('#isUpdate');
                var urlapi = '../../py/system_update.py?op=remotever&r=' + Math.random()
                $.ajax({
                    type: "POST",
                    url: urlapi,
                    dataType: 'json',
                    success: function (msg) {
                        // console.log(msg);
                        if (msg.code == '0000') {
                            var data = msg.data;
                            $('#version').text(data.localver);
                            $('#origin_version').text(data.remotever);
                            if (data.upgrade > 0) {
                                isUpdate.text('立即升级').removeAttr('disabled');
                            } else {
                                isUpdate.text('无需升级').attr({ 'disabled': 'disabled' });
                            }
                        }
                    }
                });
            })();

            Progress = function() {
                var prog = $('#prog');
                var ProgressBox = $('#ProgressBox');
                ProgressBox.show();

                // 进度条走动
                var zprogval = 0;
                var zprogval_i = 0;
                var progval = 0;
                var progval_add = 0;
                var int = self.setInterval(function(){
                    //console.log( zprogval, zprogval_i,progval,progval_add );
                    if (zprogval==0) return;
                    if (zprogval<100){
                        if (zprogval != zprogval_i){
                            progval_add = (zprogval - progval) / 300;
                            zprogval_i = zprogval;
                        }
                        if (progval>=zprogval){
                            progval = progval
                        }else{
                            progval = progval + progval_add;
                        }
                        prog.css('width', progval +'%');
                    }else{
                        prog.css('width',  '100%');
                        window.clearInterval(ajax_int);
                        window.clearInterval(int);
                        ProgressBox.hide();
                        parent.swal({title: "恭喜您！", text: '在线升级成功，系统正在后台重启，请几分钟后刷新本页面。', type: "success"});
                    }
                },500);

                var ajax_int = self.setInterval(function(){
                    $.ajax({
                        type: "GET",
                        url: '../../py/system_update.py?op=updatestate&r=' + Math.random(),
                        dataType: 'json',
                        success: function (msg) {
                            if (msg.code=='0000'){
                                zprogval = parseInt(msg.data);
                            }
                        }
                    });
                },1000);
            };

            // 开始升级操作
            $('#isUpdate').click(function(){
                var _this = $(this);
                var urlapi = '../../py/system_update.py?op=startupdate&r=' + Math.random()
                $.ajax({
                    type: "POST",
                    url: urlapi,
                    dataType: 'json',
                    success: function (msg) {
                        _this.text('正在升级…').attr({'disabled': 'disabled'});
                        Progress();
                    }
                });
            });
        });
    </script>
</body>

</html>